/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.compare.plugins');
(function (){
	var EVENTS = net.autodata.EventBus.EVENTS;
	
	function hasExistingPhantomCompetitor (index, store)
	{
		var existing = store.getById(String.format('competitor-{0}', index));
		return existing && existing.phantom;			
	}
	
	/**
	 * A plugin to connect net.autodata.compare.CompareVehiclesPanel to Session object
	 * and bind EventBus events to the Panel 
	 * @class CompareVehiclesEventBus
	 * @namespace net.autodata.compare.plugins
	 */
	var Plugin = Ext.extend(Ext.util.Observable, {
		
		/**
		 * @property panel
		 * @type net.autodata.compare.CompareVehiclesPanel
		 */
		panel: undefined,
		
		/**
		 * @method init
		 */
		init: function (panel)
		{
			this.panel = panel;
			
			this.panel.on({
				'beforechangecompetitor': this.onBeforeChangeCompetitor,
				'changecompetitor': this.onChangeCompetitor,
				'removecompetitor': this.onRemoveCompetitor,
				scope: this
			});
			
			var eb = net.autodata.EventBus.getInstance();
			
			eb.on(EVENTS.READY, this.onReady, this);
			eb.on(EVENTS.SESSION_COMPETITOR_CHANGED, this.onSessionCompetitorChanged, this);			
			eb.on(EVENTS.SESSION_COMPETITOR_REMOVED, this.onSessionCompetitorRemoved, this);
			eb.on(EVENTS.SESSION_VEHICLE_IMAGE_CHANGED, this.onSessionVehicleImageChanged, this);
		},
		
		/**
		 * Trigger the CompareVehiclePanel's store.load method
		 * @method onReady
		 * @private
		 */
		onReady: function ()
		{
			this.panel.getStore().load();
		},
		
		
		/**
		 * Do not allow the same vehicle to be selected within the compare
		 * This method checks against the base vehicle and the competitors
		 * @private
		 */
		onBeforeChangeCompetitor: function (index, acode)
		{
			//make sure the newly selected acode is not already selected in the compare
			var session = net.autodata.Session.getInstance();
			var result = session.vehicles.each(function (v) {
				if (v && v.acode === acode) {
					return false;
				}
			}, this);
			
			if (result !== undefined) {
				alert('You cannot select this vehicle as it is already selected');
				return false;
			}
			
			return true;
		},
		
		/**
		 * When the change vehicle event is fired from the CompareVehiclesPanel the new acode
		 * needs to be looked up from the CompetitorVehicleDataStore
		 * @method onChangeCompetitor
		 * @private
		 */
		onChangeCompetitor: function (index, acode)
		{	
			var cds = net.autodata.compare.data.CompetitorDataStore.getInstance();
			ASC.log(String.format('Change competitor at index {0} acode={1}', index, acode));			
			cds.loadVehicle(index, acode);
		},
		
		/**
		 * When the remove vehicle event is fired from the CompareVehiclesPanel the 
		 * CompareVehicles object stored in the client Session object needs to be updated
		 * @method onRemoveCompetitor
		 * @param {int} index
		 * @private
		 */
		onRemoveCompetitor: function (index) 
		{
			var session = net.autodata.Session.getInstance();
			ASC.log(String.format('Remove competitor at index {0}', index));
			session.vehicles.removeCompetitor(index);
		},
		
		/**
		 * When the competitorchange event is fired the CompareVehiclesPanel needs to be updated
		 * @method onSessionCompetitorChanged
		 * @private
		 */
		onSessionCompetitorChanged: function (index, competitor) 
		{
			var acode = competitor ? competitor.acode || '' : '';
			ASC.log(String.format('EVENTS.SESSION_COMPETITOR_CHANGED index={0} acode={1}', index, acode));
			
			var store = this.panel.getStore();
			
			//if we have an empty competitor check to make sure the panel does not already have
			//an empty competitor loaded in place
			var isPhantom = (!competitor || Ext.isEmpty(competitor.acode));
			if (isPhantom && hasExistingPhantomCompetitor(index, store)) {
				ASC.log('[onchanged] Existing phantom record found');
				return;
			}			
			
			this.panel.getStore().updateCompetitor(index, competitor);
		},
		
		/**
		 * When the competitorremoved event is fired the CompareVehiclesPanel needs to be updated
		 * @method onSessionCompetitorRemoved
		 * @private
		 */
		onSessionCompetitorRemoved: function (index) 
		{
			ASC.log(String.format('EVENTS.SESSION_COMPETITOR_REMOVED index={0}', index));
			
			var store = this.panel.getStore();
			
			//if we have an empty competitor check to make sure the panel does not already have
			//an empty competitor loaded in place
			if (hasExistingPhantomCompetitor(index, store)) {
				ASC.log('[onremoved] Existing phantom record found');
				return;
			}
						
			store.updateCompetitor(index, undefined);
		},
		
		onSessionVehicleImageChanged: function (vehicle)
		{
			//Need to update the store
			var store = this.panel.getStore();
			store.updateSelectedImage(vehicle.acode, vehicle.selectedImageName);
		},
		
		
		/**
		 * @method onDestroy
		 * @private
		 */
		onDestroy: function ()
		{
			var p = this.panel;
			p.un('beforechangecompetitor', this.onBeforeChangeCompetitor, this);
			p.un('changecompetitor', this.onChangeCompetitor, this);
			p.un('removecompetitor', this.onRemoveCompetitor, this);
			
			var eb = net.autodata.EventBus.getInstance();
			eb.un(EVENTS.READY, this.onReady, this);
			eb.un(EVENTS.SESSION_COMPETITOR_CHANGED, this.onSessionCompetitorChanged, this);			
			eb.un(EVENTS.SESSION_COMPETITOR_REMOVED, this.onSessionCompetitorRemoved, this);
			eb.un(EVENTS.SESSION_VEHICLE_IMAGE_CHANGED, this.onSessionVehicleImageChanged, this);
		}	
	});
	
	net.autodata.compare.plugins.CompareVehiclesEventBus = Plugin;
})();